package com.nacos.temp4.controller;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/nacos")
public class DemoRestController {

    private NacosConfigManager nacosConfigManager;

    @Autowired
    public DemoRestController(NacosConfigManager nacosConfigManager) {
        this.nacosConfigManager = nacosConfigManager;
    }

    /**
     * http://127.0.0.1:8282/nacos/configInfo
     */
    @GetMapping("/configInfo")
    public Object configInfo() {
        NacosConfigProperties nacosConfigProperties = nacosConfigManager.getNacosConfigProperties();
        return nacosConfigProperties.assembleConfigServiceProperties();
    }

    /**
     * http://127.0.0.1:8282/nacos/configData?dataId=nacos-demo4-dev.properties&group=com.potyun
     */
    @GetMapping("/configData")
    public Object configData(@RequestParam("dataId") String dataId, @RequestParam("group") String group) throws NacosException {
        final long timeout = 3000L;
        ConfigService configService = nacosConfigManager.getConfigService();
        return configService.getConfig(dataId, group, timeout);
    }

}
